package com.android.duininsuoai.fragment.member;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RatingBar;

import com.android.duininsuoai.R;
import com.android.duininsuoai.fragment.BaseLazyFragment;
import com.android.duininsuoai.model.data;
import com.android.duininsuoai.model.dataCallback;
import com.android.duininsuoai.utils.ConstantsUrl;
import com.android.duininsuoai.utils.HttpUtils;
import com.android.duininsuoai.utils.ObjectUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 评价
 */
public class Fragment_Mine_Rate extends BaseLazyFragment {


    @BindView(R.id.activity_pinjia_img)
    ImageView activityPinjiaImg;
    @BindView(R.id.activity_pinjia_RatingBar)
    RatingBar activityPinjiaRatingBar;
    @BindView(R.id.activity_pinjia_edit)
    EditText activityPinjiaEdit;

    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private static final String ARG_PARAM4 = "param4";
    private String value;
    private String thumb;
    private String buy_num;
    private String good_id;
    private String id;
    private boolean networkConnected;

    public Fragment_Mine_Rate() {


    }

    public static Fragment_Mine_Rate newInstance(String param1, String param2, String param3, String param4) {
        Fragment_Mine_Rate fragment = new Fragment_Mine_Rate();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM3, param3);
        args.putString(ARG_PARAM4, param4);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            thumb = getArguments().getString(ARG_PARAM1);
            buy_num = getArguments().getString(ARG_PARAM2);
            good_id = getArguments().getString(ARG_PARAM3);
            id = getArguments().getString(ARG_PARAM4);
        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_order_rate, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }


    private void initUI() {
        ObjectUtils.photo(mcontext, thumb, activityPinjiaImg);
        activityPinjiaRatingBar.setRating(0);

        activityPinjiaRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                activityPinjiaRatingBar.setRating(rating);
                // 选中星星的个数
                float rating1 = activityPinjiaRatingBar.getRating();
                value = String.valueOf(rating1).substring(0, 1);
            }
        });
    }


    @OnClick(R.id.activity_pinjia_finish)
    public void onClick() {
        String content = activityPinjiaEdit.getText().toString().trim();
        if (content.equals("")) {
            PostContent("未填写评价内容");
        } else {
            PostContent(content);
        }
    }

    private void PostContent(String content) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDERPINJIA;
        if (networkConnected) {
            progressLoading.setVisibility(View.VISIBLE);
            HashMap<String, String> map = new HashMap<>();
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);

            if (value == null) {
                map.put("star", "0");
            } else {
                map.put("star", value);
            }
            map.put("order_id", id);
            map.put("good_id", good_id);
            map.put("buy_num", buy_num);
            map.put("content", content);

            HttpUtils.post(address, header, map, new dataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    progressLoading.setVisibility(View.GONE);
                }

                @Override
                public void onResponse(data response, int id) {
                    progressLoading.setVisibility(View.GONE);
                    ObjectUtils.toast(mcontext, response.getMsg());
                    if (response.getCode() == 0) {
                        Intent mIntent = new Intent(Fragment_Mine_DirectBuy_Order.DIRECTBUY_ORDER_COMMENT);
                        mcontext.sendBroadcast(mIntent);
                        mcontext.finish();
                    }
                }
            });
        } else {
            ObjectUtils.toast(mcontext, "网络连接出现异常");
        }
    }
}
